﻿var path = require('path');
var webpack = require("webpack");
var merge = require('webpack-merge');
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var HtmlWebpackPlugin = require('html-webpack-plugin');

var T = process.env.npm_lifecycle_event

//定义了一些文件夹的路径
var ROOT_PATH = path.resolve(__dirname);
var APP_PATH = path.resolve(ROOT_PATH, 'src/app.js');
var BUILD_PATH = path.resolve(ROOT_PATH, 'dist');
var plugins = [
      //new CleanPlugin('dist'),
      new ExtractTextPlugin("css/[name].[hash:5].css",{ 
          allChunks:true
      }),
      new webpack.optimize.CommonsChunkPlugin('js/common.js'),
      new HtmlWebpackPlugin({  // 首页 
        filename: 'index.html',
        template: path.resolve(ROOT_PATH, './src/index.html')
      })
];
var common = {
    // 入口文件，path.resolve()方法，可以结合我们给定的两个参数最后生成绝对路径，最终指向的就是我们的index.js文件
    entry: {main:APP_PATH},
    // 输出配置
    output: {
        // 输出路径是 myProject/output/static
        path: BUILD_PATH,
        // publicPath: 'http://127.0.0.1:8080/',
       // publicPath: '',
        filename: 'js/[name].[hash:5].js'
    },
    resolve: {
        extensions: ['', '.js', '.vue'],
        alias:{
        	'vue': 'vue/dist/vue.js'
        }
    },
    module: {
      loaders: [
	      	  { test: /\.js$/, exclude: /node_modules/, loader: 'babel' },
	          { test: /\.vue$/, loader: 'vue' },
	          {
		        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
		        loader: 'url',
		        query: {
		          limit: 10000,
		          name: path.posix.join(__dirname, 'public', '[name].[hash:7].[ext]')
		        }
		      },
		      { test:/\.css$/, loader: ExtractTextPlugin.extract("style-loader", "css-loader")},
		      //图片文件使用 url-loader 来处理，小于8kb的直接转为base64
		      {
		      	test: /\.(jpg|jpe?g|png|gif|svg)$/,
		      	loader: 'url',
		      	query: {
		      		limit: 10000,
		      		name: 'imgs/[name].[ext]'
		      	},
		      	exclude: /node_modules/,
		      	include: path.resolve(__dirname, './src/imgs/')
		      },
		      {
		          test: /\.woff(\?v=\d+\.\d+\.\d+)?$/,
		          loader: "url",
		          query: {
		            name: '[name].[ext]?[hash]&mimetype=application/font-woff'
		          }
		      },
		      {
		          test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/,
		          loader: "url",
		          query: {
		            name: '[name].[ext]?[hash]&mimetype=application/font-woff2'
		          }
		      },
		      {
		          test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
		          loader: "url",
		          query: {
		            name: '[name].[ext]?mimetype=application/font-woff2'
		          }
		      },
		      {
		          test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
		          loader: "url",
		          query: {
		            name: '[name].[ext]?mimetype=application/font-woff2'
		          }
		      },
		      {
		          test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
		          loader: "url",
		          query: {
		            name: '[name].[ext]?mimetype=image/svg+xml'
		          }
		      },
		      {
		          test: /\.json(\?v=\d+\.\d+\.\d+)?$/,
		          loader: "url",
		          query: {
		            name: '[name].[ext]?mimetype=application/json'
		          }
		      }
            ]
        
    },
    // 转化成es5的语法
    babel: {
      presets: ['es2015','stage-3'],
      plugins: ['transform-runtime']
    },
    plugins: plugins
}
if (T === 'dev' || !T) {
  var config = merge(common, {
    devServer: {
      historyApiFallback: true,
      hot: true,
      inline: true,
      progress: true,
      stats: 'errors-only',
      host: process.env.HOST || 'localhost',
      port: process.env.PORT || '8080'
    },
    devtool: 'inline-source-map',
    plugins: [
      new webpack.HotModuleReplacementPlugin()
    ]
  })

  config.entry.main = ['webpack/hot/dev-server', config.entry.main]
  module.exports = config
}
if (T === 'build') {
  module.exports = merge(common, {})
}